package com.clps.deadLock;

public class DeadLock01 {
    public static void main(String[] args) {
        Resource666 res1 = new Resource666();
        Resource666 res2 = new Resource666();
        res1.flag = true;
        res2.flag = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                res1.method();
            }
        }, "A").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                res2.method();
            }
        },"B").start();
    }
}
class Resource666{
    boolean flag = true;
    private static  final  Object obj1 = new Object();
    private static  final  Object obj2 = new Object();

    public  void method(){
        if(flag){
            synchronized (obj1){
                System.out.println(Thread.currentThread().getName()+"---"+"获取锁1");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                synchronized (obj2){
                    System.out.println(Thread.currentThread().getName()+""+"获取到锁2");
                }
            }
        }else{
            synchronized (obj2){
                System.out.println(Thread.currentThread().getName()+"获取到锁2");
                synchronized (obj1){
                    System.out.println(Thread.currentThread().getName()+"获取到锁2");
                }
            }
        }
    }


}